import sys,os,cgi,time
sys.path.insert(0, os.path.split(sys.path[0])[0])

query = os.getenv("QUERY_STRING",'') 
kwargs = cgi.parse_qs(query)
run_profiler = int(kwargs.get('profile',['0'])[0])

def main():
	import cnd
	import job
	import perf
	import res
	
	import re

	def iso(d):
		return '-'.join([d[:4],d[4:6],d[6:8]])

	def status_color(status_set):
		ss=status_set
		if 'rerun' in ss:
			return '#00FFFF'
		elif 'ok' in ss:
			return '#00FF00'
		elif 'error' in ss:
			return '#FF0000'
		elif 'run' in ss:
			return '#00FFFF'
		else:
			return '#FFFFFF'

	query = os.getenv("QUERY_STRING",'') 
	kwargs = cgi.parse_qs(query)
	date = kwargs.get('date',[time.strftime("%Y%m%d")])[0]
	tags = [t.lower() for t in re.split("\s+",kwargs.get('tags',[''])[0]) if t]
	job_name = kwargs.get('job',[''])[0]
	date_iso = iso(date)
	past_dist=int(kwargs.get('past',['1'])[0])
	future_dist=int(kwargs.get('future',['1'])[0])

	j = job.Job(job_name,date,fake=True)

	if False:
		print "HTTP/1.1 200 ok"
		print "Content-Type: text/html"     # HTML is following
		print
		print "<title>%s %s</title>" % (job_name,date_iso)
		print """<html><head>
		<STYLE>
		<!--
		a {text-decoration:none}
		//-->
		</STYLE>
		</head>"""
	else:
		import head
		head.header()

	print "<body>"

	# header
	print """<h1><a href="status.py?date=%s">%s</a> %s flow</h1>""" % (date,date_iso,job_name)

	print '<form name="flow" method="GET" action="action.py">'
	
	print '<table border="1" cellpadding="5" cellspacing="0">'
	print "<tr><th>dist</th> <th>job</th> <th>date</th> <th>status</th> <th>use</th></tr>"

	more_a = '<a href="flow.py?date=%s&job=%s&past=%s&future=%s">more</a>' % (date,job_name,past_dist+1,future_dist)
	print "<tr><td>%s</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>" % more_a

	# past
	for dist,jd_set in sorted(j.past_flow(past_dist).items(),reverse=True):
		for n,d in jd_set:
			dist_a = '<a href="flow.py?date=%s&job=%s">%s</a>' % (d,n,-dist)
			job_a = '<a href="job.py?date=%s&job=%s">%s</a>' % (d,n,n)
			date_a = '<a href="status.py?date=%s">%s</a>' % (d,iso(d))
			status_set = set([x[len(n)+1:] for x in cnd.list(d,n+'.')])
			status = "<br>".join(sorted(status_set))
			sel = '<input type="checkbox" name="%s" value="%s">' % (n,d)
			print "<tr><td>%s</td><td>%s</td><td>%s</td><td bgcolor=%s>%s</td><td>%s</td></tr>" % (dist_a,job_a,date_a,status_color(status_set),status,sel)
	# job
	dist_a = '<a href="flow.py?date=%s&job=%s">%s</a>' % (date,job_name,0)
	job_a = '<a href="job.py?date=%s&job=%s">%s</a>' % (date,job_name,job_name)
	date_a = '<a href="status.py?date=%s">%s</a>' % (date,iso(date))
	status_set = set([x[len(job_name)+1:] for x in cnd.list(date,job_name+'.')])
	status = "<br>".join(sorted(status_set))
	sel = '<input type="checkbox" name="%s" value="%s">' % (job_name,date)
	print "<tr><td>%s</td><td>%s</td><td>%s</td><td bgcolor=%s>%s</td><td>%s</td></tr>" % (dist_a,job_a,date_a,status_color(status_set),status,sel)
	# future
	for dist,jd_set in sorted(j.future_flow(future_dist).items()):
		for n,d in jd_set:
			dist_a = '<a href="flow.py?date=%s&job=%s">%s</a>' % (d,n,dist)
			job_a = '<a href="job.py?date=%s&job=%s">%s</a>' % (d,n,n)
			date_a = '<a href="status.py?date=%s">%s</a>' % (d,iso(d))
			status_set = set([x[len(n)+1:] for x in cnd.list(d,n+'.')])
			status = "<br>".join(sorted(status_set))
			sel = '<input type="checkbox" name="%s" value="%s">' % (n,d)
			print "<tr><td>%s</td><td>%s</td><td>%s</td><td bgcolor=%s>%s</td><td>%s</td></tr>" % (dist_a,job_a,date_a,status_color(status_set),status,sel)

	more_a = '<a href="flow.py?date=%s&job=%s&past=%s&future=%s">more</a>' % (date,job_name,past_dist,future_dist+1)
	print "<tr><td>%s</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>" % more_a

	print "</table>"
	print "<br>"
	print '<input type="text" name="kind">'
	print '<input type="submit" value="add" name="action">'
	print '<input type="submit" value="remove" name="action">'
	print "</form>"
	print ".</body></html>"

if run_profiler:
	import profile
	profile.run("main()","rivulet-cgi-flow.profile.stats")
else:
	main()
